package ${projectPackage};

import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.CookieHandler;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.sstore.LocalSessionStore;
import ldh.market.config.AppConfiguration;
import ldh.market.util.VertxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* Created by ldh123 on 2018/4/28.
*/
@Component
public class MainHttpServer extends AbstractVerticle{

    @Autowired
    AppConfiguration configuration;

    public void start() {
        HttpServer server = vertx.createHttpServer();

        Router router = Router.router(vertx);

        router.route().handler(CookieHandler.create());
        router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
        //        router.route().handler(StaticHandler.create());
        router.route("/resource/*").handler(StaticHandler.create("resource").setCachingEnabled(true));
        router.route("/*").handler(StaticHandler.create("landing").setCachingEnabled(true));
        //        router.route().handler(CORSHandler.create());
        //        router.route().handler(LogHandler.create());

        VertxUtil.setRouter(router);

        server.requestHandler(router::accept).listen(configuration.httpPort());
    }
}
